package com.luisliuyi.demo.optimize.makechannel;


import com.luisliuyi.lib.apk.ApkBuilder;
import com.luisliuyi.lib.apk.ApkParser;
import com.luisliuyi.lib.apk.data.Apk;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws Exception {
        long l = System.currentTimeMillis();
        /**
         * 1、初始化:创建输出目录、读取渠道文件
         */
        File baseApk = new File("makechannel/output/app-debug.apk");
        File outDir = new File("makechannel/output");
        outDir.mkdirs();

        /**
         * 2、解析APK(zip文件)
         * 将整个fiel解析成一个APK对象
         */
        Apk apk = ApkParser.parser(baseApk);

        /**
         * 3、生成APK
         */
        File channelFile = new File("makechannel/channel.txt");
        List<String> channels = readChannelFile(channelFile);

        String name = baseApk.getName();
        name = name.substring(0, name.lastIndexOf("."));
        System.out.println(name);
        for (String channel : channels) {
            File file = new File(outDir, name + "-" + channel +
                    ".apk");
            ApkBuilder.generateChannel(channel, apk, file);
        }

        System.out.println(System.currentTimeMillis() - l);

    }

    private static List<String> readChannelFile(File channelFile) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(new
                FileInputStream(channelFile)));
        List<String> list = new ArrayList<>();
        String line;
        while ((line = br.readLine()) != null) {
            list.add(line);
        }
        return list;
    }
}
